home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / obsolete / multicompare.pro < prev    next >
Text File  |  1997-07-08  |  11KB  |  381 lines

  1. ; $Id: multicompare.pro,v 1.2 1997/01/15 04:02:19 ali Exp $
  2. ;
  3. ;  Copyright (c) 1991-1997, Research Systems Inc.  All rights
  4. ;  reserved. Unauthorized reproduction prohibited.
  5.  
  6.  
  7. function testmeans, A
  8. SX = size(A)
  9. if SX(0) eq 1 THEN CNum = 0 else CNum = SX(2) - 1
  10. for i = long(0), CNum do BEGIN
  11. here = where(A(*,i) NE 0,count)
  12. if count NE 2 THEN return,0
  13. ENDFOR
  14. return,1
  15. END
  16.   
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  pro multicompare, X, Contrast, a, ST,One_Way = One, Unequal_One_Way = Unequal,$
  23.             Two_Way = Two, Interactions_Two_Way  = $
  24.             Interactions,LSD = LS, Bonferroni=Bon,Scheffe = Sch, Tukey = Tuk,$
  25.             Missing = M, Block=B, TInteraction = IN
  26. ;+
  27. ; NAME: 
  28. ;    MULTICOMPARE
  29. ;
  30. ; PURPOSE:
  31. ;    Multicompare gives the user access to a variety of procedures for 
  32. ;    making many inferences from a single experimental data set. These
  33. ;    procedures are designed to guard against experimentwise errors 
  34. ;    resulting from the increased probabilty of at least one inferential
  35. ;    error when many inferences are made. 
  36. ;
  37. ; CATEGORY: 
  38. ;    Statistics.
  39. ;
  40. ; CALLING SEQUENCE:
  41. ;    MULTICOMPARE, X, Contrast, A, St
  42. ;
  43. ; INPUTS: 
  44. ;    X:    2 or 3-dimensional array of experimental data values.
  45. ;
  46. ;     Contrast:    An array, dimensioned (CL,C), where:
  47. ;        CL = the number of treatments
  48. ;           = the number of columns of X
  49. ;        and 
  50. ;        C = the number of contrasts or inferences to be tested.
  51. ;        Contrast, B(i,j) = the coefficient of the mean of the ith 
  52. ;        treatment in the jth contrast.
  53. ;
  54. ;    A:    Experimentwise significance level desired. 
  55. ; OUTPUT:
  56. ;    St:    An array, dimensioned (2,C), where C is the number
  57. ;        of contrasts to test. ST(0,j) = the absolute value
  58. ;        of the test statistic. ST(1,j) = the lower
  59. ;        limit of the rejection region - i.e.,
  60. ;        if ST(0,j) > ST(1,j) reject the null hypothesis for
  61. ;        the jth contrast at the a*100% significance level.
  62. ;
  63. ; KEYWORD PARAMETERS:
  64. ;    Type of design structure. Options are:
  65. ;      ONE_WAY:    If set,  1-way anova.
  66. ;
  67. ; UNEQUAL_ONE_WAY:  If set,  1-way ANOVA with unequal sample sizes.
  68. ;
  69. ;      TWO_WAY:    If set,  2-way ANOVA.
  70. ;
  71. ; INTERACTIONS_TWO_WAY:    If set, 2-way ANOVA with interactions.
  72. ;    One and only one of the above options may be set.
  73. ;
  74. ;    Options for multicomparison testing:
  75. ;
  76. ;    LSD:    Fisher's LSD procedure. LSD is a post-hoc test of any
  77. ;        contrasts of the treatment means.  It should only be used if
  78. ;        the F-test for equal means rejects the null hypothesis at
  79. ;        the A*100% significance level. LSD has an experimentwise
  80. ;        error rate approximately equal to 5%.
  81. ;
  82. ;   BONFERRONI:    Bonferroni's method. This method should be selected
  83. ;        instead of LSD if the F-test is not significant.
  84. ;                                      
  85. ;        
  86. ;      SCHEFFE:    Scheffe's procedure.  Use Sheffe's procedure to make any
  87. ;        number of unplanned comparisons - that is, to data snoop.
  88. ;        Experimentwise error rate is A*100%.              
  89. ;
  90. ;    TUKEY:    Tukey`s procedure. Tukey's procedure is often more sensitive
  91. ;        than Sheffe's but in the general case it requires equal sample
  92. ;        sizes. Pairwise testing of means is allowed with unequal 
  93. ;        sample sizes, but if the disparity is too great, Sheffe's 
  94. ;        method is more sensitive.  The experimentwise error rate, A,
  95. ;        must be between 0 and 0.1.
  96. ;
  97. ;    BLOCK:    a flag which ,if set, signals that comparisons
  98. ;        should be done on block means instead of treatment means.
  99. ;        Alternatively, input transpose(X) for the experimental data
  100. ;        array.
  101. ;
  102. ;      MISSING:    Placeholding value for missing data.  If not set, then assume
  103. ;        no missing data.
  104. ;
  105. ; TINTERACTION:    A flag, if set, to signal that contrasts are for interaction
  106. ;        effects. The user should not set both keywords BLOCK and
  107. ;        TINTERACTION.
  108. ;-
  109. On_Error,2
  110.  
  111. ;  Ascertain design structure
  112.  ONE_WAY = 0 & ONE_WAY_UNEQUAL=1 & TWO_WAY = 2 & TWO_WAY_INTERACTION = 3
  113.  
  114.  keyset = [keyword_set(One), keyword_set(Unequal), $
  115.             keyword_set(Two),keyword_set(Interactions)]
  116.  T  = where(keyset,nkey)
  117.  
  118.  if nkey NE 1 THEN BEGIN
  119.     print, $
  120.          'multicompare - must specify one and only type of '
  121.     print,'               of design structure'
  122.     goto,DONE
  123.  ENDIF
  124.  
  125.  T = T(0)      ; T = selected design structure
  126.  
  127.  
  128. ; Ascertain test type
  129.  LSD = 0 & BONFERRONI = 1 & Scheffe = 2 & TUKEY = 3
  130.  
  131.  keyset = [keyword_set(LS), keyword_set(Bon), $
  132.            keyword_set(Sch),keyword_set(TUK)]
  133.  MT  = where(keyset,nkey)
  134.  
  135.  if nkey NE 1 THEN BEGIN
  136.     print,'multicompare - must specify one and only one type'
  137.    print, '               of multiple comparison test'
  138.    goto,DONE
  139.  ENDIF
  140.  
  141.  MT = MT(0)    ; M = selected test type
  142.  
  143.  
  144. If N_Params(0) LT 3 THEN BEGIN
  145.   print,"multicompare- need more parameters."
  146.   goto, done
  147. ENDIF
  148.  
  149.  
  150. if (a GT 1.0 OR a LT 0) THEN BEGIN
  151.     print,        $
  152.      'multicompare- significance level a must lie between 0 and 1'
  153.     goto, DONE
  154.  ENDIF ELSE if MT EQ TUKEY and a GT .1 THEN BEGIN
  155.       print, "multicompare - experiment-wise error rate must be"  
  156.       print, "               no greater than .1 for Tukey's test"
  157.       goto,done
  158.      ENDIF
  159.  
  160.  X1 = float(X)
  161.  SX = size(X1)
  162.  
  163.  if (SX(0) NE 2 AND T ne TWO_WAY_INTERACTIONS) OR   $
  164.     (SX(0) ne 3 and T eq TWO_WAY_INTERACTIONS) THEN BEGIN
  165.     print,      $
  166.      'multicompare- data array has wrong number of dimensions'
  167.     goto,done
  168.  ENDIF
  169.  
  170. if (KEYWORD_SET(B) NE 0) THEN    $ 
  171.                         ;transpose to test block means
  172.   if (T NE TWO_WAY_INTERACTIONS) THEN      $
  173.        X1 = transpose(X1)        $
  174.   ELSE BEGIN
  175.        SX = size(X1)
  176.        X1 = fltarr(SX(3),SX(2),SX(1))
  177.        for i = 0l,SX(1) -1 do $
  178.          for j = 0l,SX(3) -1 do $
  179.             X1(j,*,i) = float(X(i,*,J))
  180.   ENDELSE
  181.  
  182.  
  183.  SX =size(X1)
  184.  SC = size(Contrast)
  185.  C = SX(1)
  186.  CN = SC(1)
  187.   
  188.  
  189.  
  190.  
  191.  
  192. ; If testing interactions, reform array and perform 1-way testing
  193.  
  194. if KEYWORD_SET(IN) THEN BEGIN
  195.  if KEYWORD_SET(B) THEN BEGIN
  196.   print,   $
  197.     "multicompare - keywords Block and Interaction should not both be set."
  198.   goto,done
  199.  ENDIF 
  200.  
  201.  if (T EQ TWO_WAY_INTERACTIONS) THEN $
  202.    if (SX(0) NE 3) THEN BEGIN
  203.     print, 'multicompare- wrong number of dimensions.' 
  204.     goto,DONE
  205.    ENDIF ELSE BEGIN                ;reform array for one-way anova
  206.    X1 = Fltarr(C*SX(3),SX(2))
  207.    for i = 0,SX(3) -1 DO $
  208.     for j = 0,C-1 DO $
  209.      X1(C * i +j,*) = float(X(j,*,i))
  210.    SX = size(X1)
  211.    C  = SX(1)
  212.    T = ONE_WAY
  213.   ENDELSE     $ 
  214.  ELSE BEGIN
  215.    print,    $
  216.  "multicompare- to compare interaction effects, keyword structure"
  217.   print,"                      must be set to TWO_WAY_INTERACTIONS "
  218.    goto, done
  219.  ENDELSE
  220. ENDIF
  221.  
  222.  
  223.  if ( SC(0) EQ 1) THEN CNUM = 1 else CNUM = SC(2)
  224.  
  225.  if (C NE CN) THEN BEGIN
  226.    print,         $
  227.    'multicompare- data and contrast arrays must have same number of columns'
  228.    goto,DONE
  229.  ENDIF
  230.  
  231.  if testcontrast(Contrast) EQ 0 THEN BEGIN
  232.   print,'multicompare- Contrast array contains equations whose coefficients'
  233.   print,'              do not sum to 0'
  234.   return
  235.  ENDIF
  236.  
  237.  D=1
  238.  if T NE TWO_WAY_INTERACTIONS THEN  R=SX(2) ELSE BEGIN
  239.     R=SX(3) 
  240.     D=SX(2)    
  241.  ENDELSE                                  ;Compute number of rows
  242.  
  243.  
  244.  ; replace missing data with 0
  245.  if ( T EQ ONE_WAY_UNEQUAL and N_ELEMENTS(M) NE 0) THEN BEGIN
  246.  
  247.      Pairwise,X1,M,YR,YC, nogood, good
  248.      CN = where(YC NE 0,NYC)
  249.      RN = where(YR NE 0,NYR)
  250.      if NYC LT 2 OR NYR LT 2 THEN BEGIN
  251.        print,"multicompare, too many missing entries"
  252.       return 
  253.     ENDIF
  254.  
  255.      YC = float(YC)
  256.  
  257.         ; compute sqd dev of columns from their means
  258.      Mean =  (X1 # Replicate(1.,R)) /YC  
  259.      Mean = MEAN # replicate(1.0, R)
  260.      sigma = fltarr(C,R)
  261.      sigma(good) = (X1(good) - Mean(good))^2
  262.      sigma = sigma # Replicate(1.0,R)
  263.  
  264.  ENDIF ELSE    BEGIN
  265.  
  266.          YC = float(R)
  267.         if (T EQ TWO_WAY_INTERACTIONS) THEN BEGIN    
  268.  
  269.           YC = YC * D
  270.           X2 = X1
  271.           X1= reform(X1,C,YC)
  272.   
  273.         ; Compute sum squared deviations of columns from their means 
  274.           Mean =  (X1 # replicate(1.0,YC))/YC
  275.           sigma = (X1 -  MEAN # replicate(1.0, YC))^2 
  276.           sigma = sigma # replicate(1.0,YC)
  277.  
  278.         ;Pre-process three dimensional are by converting to a two
  279.         ; dimensional array whose entries are sums of repetitions
  280.  
  281.         X1 = fltarr(C,R)
  282.         for i = float(0),R-1 Do   $
  283.         for j=0,C-1 DO        $
  284.           X1(j,i) = Total(X2(j,*,i)) 
  285.                           
  286.       ENDIF ELSE BEGIN
  287.        Mean = (X1 # replicate(1.0, YC))/YC
  288.        sigma = (X1 -  MEAN # replicate(1.0, YC))^2 
  289.        sigma = sigma # replicate(1.0,YC)
  290.       ENDELSE
  291.   ENDELSE
  292.  
  293.  
  294.  
  295.  
  296. if ( T EQ ONE_WAY_UNEQUAL AND N_ELEMENTS(M) ne 0) THEN         $
  297.                sigma =sqrt( total(sigma)/(Total(YC)-C))           $
  298. ELSE sigma =sqrt( total(sigma)/(C*YC-C)) 
  299.  
  300.  
  301. Means = (X1#replicate(1.0,R))/YC      ;compute column means
  302. ST = fltarr(2,CNUM)
  303.  
  304. if CNUM GE 2 THEN ST(0,*) = ABS(MEANS # Contrast)  $
  305.  ELSE   ST(0) = ABS(Total(Contrast * Means))
  306.  
  307. CASE MT of
  308.         ; compute test statistic for all examples
  309.         ; based on contrasts
  310.  TUKEY: BEGIN
  311.          If T EQ ONE_WAY_UNEQUAL AND   $
  312.           testmeans(Contrast) EQ 0 THEN BEGIN
  313.           print, 'Multicompare- TUKEY test can only handle'
  314.           print, '              missing data when comparing means'
  315.           goto,done
  316.          ENDIF
  317.  
  318.         if CNUM GE 2 THEN BEGIN 
  319.           if ( T NE ONE_WAY_UNEQUAL OR N_ELEMENTS(M) eq 0) THEN  BEGIN
  320.             Test_Stat =Replicate(1.0,C)   $
  321.                        #ABS(Contrast)/sqrt(YC)   
  322.             ST(1,*) =  sigma * Test_Stat
  323.           ENDIF ELSE BEGIN
  324.  
  325.             for i = 0l,CNUM - 1 DO   $
  326.              ST(1,i) = min (YC(where(Contrast(*,i) NE 0)))
  327.             ST(1,*) =2.0* sigma / sqrt(ST(1,*))
  328.           ENDELSE
  329.        ENDIF ELSE BEGIN
  330.            if T EQ ONE_WAY_UNEQUAL THEN $
  331.             YC1 =  min(YC(where(Contrast ne 0))) $
  332.            else YC1 = YC
  333.            ST(1) = sigma*Total(ABS(Contrast)/sqrt(YC1))
  334.          ENDELSE
  335.   END
  336.  
  337. ELSE: BEGIN
  338.     if CNUM GE 2 THEN BEGIN 
  339.       ;compute test statistic for all examples based on contrasts
  340.     if ( T NE ONE_WAY_UNEQUAL or N_ELEMENTS(M) eq 0) THEN       $
  341.      Test_Stat =Replicate(1.0/YC,C)# Contrast^2   $
  342.     ELSE Test_Stat = 1./YC # Contrast^2
  343.     ST(1,*) = sigma * (sqrt(Test_Stat))
  344.     ENDIF ELSE  $
  345.       ST(1) = sigma*(sqrt(Total(Contrast^2/YC)))
  346.     END
  347. ENDCASE
  348.  
  349.  
  350.  if( N_ELEMENTS(YC) EQ 1) then v = YC*C-C else v = total(YC) -C
  351.  
  352.  case MT of
  353.   
  354.       LSD       : tav = student_t (a/2.,v)
  355.       BONFERRONI:BEGIN
  356.                    tav = student_t(a/(CNUM*2),v)
  357.                    END
  358.       SCHEFFE    : Begin
  359.                      tav =F_test(a,C-1,v)  
  360.                      tav=sqrt((C-1)*tav) 
  361.                      END
  362.        TUKEY:    if C ne 2 THEN $
  363.                   tav = .5 * studrange(1.0 - a,v,C)   $
  364.                 else tav = sqrt(2)*student_t(a/2.,v)/2.
  365.               
  366.        ELSE: BEGIN
  367.             print,"multicompare- unknown Test selected"
  368.             goto,DONE
  369.             END
  370.  ENDCASE
  371.  
  372.  
  373.  
  374. ST(1,*) = tav*ST(1,*)   
  375.  
  376.  
  377. DONE:
  378. RETURN
  379. END   
  380.